package com.zimin.cloud.web.controller;

import com.zimin.redis.constant.BaseRedisConstant;
import com.zimin.redis.utils.RedisOperator;
import com.zimin.shared.annotation.RedisLock;
import com.zimin.shared.pojo.Result;
import org.n3r.idworker.Sid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import reactor.core.publisher.Mono;

/**
 * 公共的controller
 *
 * @author zimin
 * @since 2021/10/15 11:23 上午
 */
@RestController
@RequestMapping("/common")
public class CommonController {

    @Autowired
    private RedisOperator redisOperator;
    @Autowired
    private Sid sid;


    /**
     * <p>生成幂等性</p>
     *
     * @param
     * @return java.lang.String
     * @author zimin
     * @since 2021-11-04 15:19:21
     */
    @GetMapping("/idempotence")
    public Mono<String> idempotence() {
        //TODO:生成幂等性的key
        String idempotencePrefix = BaseRedisConstant.IDEMPOTENCE_PREFIX;
        String key = String.format(idempotencePrefix, sid.nextShort());
        redisOperator.setIdempotence(key);
        return Mono.just(key);
    }

    @GetMapping("/test/redis/lock")
    @RedisLock
    public Mono<Result> testRedisLock(){
        return Mono.just(Result.ok());
    }


}
